package com.zyx.educenter.controller;


import com.zyx.commonutils.JwtUtils;
import com.zyx.commonutils.R;
import com.zyx.commonutils.ordervo.UcenterMemberOrder;
import com.zyx.educenter.entity.UcenterMember;
import com.zyx.educenter.entity.vo.RegisterVo;
import com.zyx.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zyx
 * @since 2022-01-08
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Resource
    private UcenterMemberService memberService;

    /**
     * 登录
     * @param ucenterMember
     * @return
     */
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember ucenterMember) {

        String token = memberService.login(ucenterMember);

        return R.ok().data("token",token);
    }

    /**
     * 注册
     */
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 根据Token获取用户信息
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){

        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(id);

        return R.ok().data("userInfo",member);
    }

    /**
     * 根据用户id获取用户信息
     */
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){

        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);

        return ucenterMemberOrder;
    }

    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){

        Integer count = memberService.countRegister(day);

        return R.ok().data("countRegister",count);
    }

}

